package com.birdboot.core;

import com.birdboot.http.HttpServletRequest;
import com.birdboot.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.URISyntaxException;
import java.util.*;

public class ClientHandler implements Runnable{

    ClientHandler(Socket socket){
        this.socket = socket;
    }
    private Socket socket;
    @Override
    public void run() {
        try {
            //创建请求与回复类
            HttpServletRequest httpRequest = new HttpServletRequest(socket);
            HttpServletResponse httpResponse = new HttpServletResponse(socket);

            DispatcherServlet dispatcherServlet = new DispatcherServlet();
            dispatcherServlet.service(httpRequest,httpResponse);
            //获取请求的URI路径
/*            System.out.println("页面请求地址："+httpRequest.getUri());
            String path = httpRequest.getUri();
            File baseDir = new File(
                    ClientHandler.class.getClassLoader().getResource(".").toURI()
            );
            File staticDir = new File(baseDir,"static");
            File file = new File(staticDir,path);
            System.out.println("文件是否存在"+ file.exists()+" "+file.getPath().toString());
            if(file.exists() && file.isFile()){
                httpResponse.setContentFile(file);
            }else{
                httpResponse.setCodeStatus(404);
                httpResponse.setStatusReason("NOTFOUND");
                File fileError = new File(staticDir,"404.html");
                System.out.println(fileError.getPath());
                httpResponse.setContentFile(fileError);
            }
            httpResponse.Response();*/
        } catch (IOException  e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
